package com.appspot.modutajataja;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;

import com.facebook.Request;
import com.facebook.Response;
import com.facebook.Session;
import com.facebook.SessionState;
import com.facebook.model.GraphUser;

public class IntroActivity extends Activity {
	private String userId;
	private String userEmail;	

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_intro);

		// start Facebook Login
		Session.openActiveSession(this, true, new Session.StatusCallback() {

			// callback when session changes state
			@Override
			public void call(Session session, SessionState state, Exception exception) {
				if (session.isOpened()) {
					// make request to the /me API
					Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {

						// callback after Graph API response with user object
						@Override
						public void onCompleted(GraphUser user, Response response) {
							if (user != null) {
								
								// 유저 ID 가져오기
								if(user.getProperty("username") != null) {
									userId = user.getProperty("username").toString();
								} else {
									userId = user.getName();
								}
								
								//유저 Email 가져오기
								if(user.getProperty("email") != null) {
									userEmail = user.getProperty("email").toString();						
								} else {
									userEmail = userId + "@facebook.com";
								}
								
								new userInsert().execute();
							
								startActivity(new Intent(IntroActivity.this, MainActivity.class).putExtra("userId", userId));	
								overridePendingTransition(R.anim.fade, R.anim.hold);
								IntroActivity.this.finish();	
							}
						}
					});
				}
			}
		});
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
	}

	class userInsert extends AsyncTask<Void, Void, Void> {
		public userInsert() { ; }

		@Override
		protected Void doInBackground(Void... params) {
			try {
				HttpClient client = new DefaultHttpClient();
				HttpPost post = new HttpPost(C.userInfoURL);

				List<NameValuePair> userList = new ArrayList<NameValuePair>();
				userList.add(new BasicNameValuePair("userId", userId));
				userList.add(new BasicNameValuePair("userEmail", userEmail));
				
				post.setEntity(new UrlEncodedFormEntity(userList, HTTP.UTF_8));	
				HttpResponse response = client.execute(post);
			} catch (Exception e) {
				e.printStackTrace();			
			}
			return null;
		}
	}
};